package com.yehee.chainserver.common.rest;


import com.yehee.chainserver.common.constant.CoinType;
import com.yehee.chainserver.common.entity.BaseResponse;
import com.yehee.chainserver.common.entity.ChainWithdrawResponse;
import com.yehee.chainserver.common.entity.WalletConfig;
import com.yehee.chainserver.core.EthCore;
import com.yehee.chainserver.mapper.EthWalletMapper;
import com.yehee.chainserver.mapper.WalletConfigMapper;
import com.yehee.chainserver.server.EthChainServer;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.yehee.chainserver.common.constant.CoinType.ETH_TYPE;

@RestController
@RequestMapping("/chain")
@Log4j2
public class ChainServer {
    @Autowired
    WalletConfigMapper walletConfigMapper;

    @Autowired
    EthCore ethCore;
    @Autowired
    EthChainServer ethChainServer;
    @Autowired
    EthWalletMapper ethWalletMapper;

    @RequestMapping(value = "/newAccount/{userId}/{symbol}")

     public   BaseResponse newAccount(@PathVariable String symbol,@PathVariable int userId)
    {

        symbol=symbol.toUpperCase();
        WalletConfig walletConfig= walletConfigMapper.selectBySymbol(symbol);
        String coinType= walletConfig.getCoinType().toUpperCase();

        switch (coinType){
            case "ETH":{
                try {
                    String address=ethChainServer.newAccount(symbol);
                    ethWalletMapper.setAddress(userId,address);
                    return BaseResponse.makeBaseResponse(200,address);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                    return BaseResponse.makeBaseResponse(500,throwable.getMessage());
                }
            }
        }

        return  BaseResponse.makeBaseResponse(500,"无相关币种");


    }

    @RequestMapping(value = "withdraw")
    public   BaseResponse withdraw(BigDecimal amout ,String symbol,String toAddress,String memo)
    {

        WalletConfig walletConfig= walletConfigMapper.selectBySymbol(symbol);
        String coinType= walletConfig.getCoinType().toUpperCase();

        switch (coinType){
            case "ETH":{
                try {
                    ChainWithdrawResponse  response=ethChainServer.withdraw(symbol,toAddress,amout,memo);
                    return BaseResponse.makeBaseResponse(200,response);
                } catch (Throwable throwable) {
                    return BaseResponse.makeBaseResponse(500,throwable.getMessage());
                }
            }
        }

        return  BaseResponse.makeBaseResponse(500,"无相关币种");


    }
    ExecutorService executorService = Executors.newFixedThreadPool(40);

    @RequestMapping("ethScan")
    public BaseResponse ethScan(long start,long end) {
        for (long i = start; i <= end ; i++) {
            try {
                log.info("手动扫描区块: "+i);
                ethChainServer.scanBlock(i,executorService);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return BaseResponse.ok();
    }



}
